package geez.widget;

import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.ui.ListBox;

import static geez.widget.Geez.withStyle;

public class GListBox extends ListBox implements GWidget, GClickable, GChangable, GFocusable {

    public GListBox(boolean multiple) {
        super(multiple);
    }

    public GListBox id(String id) {
        Geez.withNameAndId(this, id);
        return this;
    }

    public GListBox name(String name) {
        setName(name);
        return this;
    }

    public GListBox css(String cssClass, String... optionalClasses) {
        withStyle(this, cssClass, optionalClasses);
        return this;
    }

    public GWidget show() {
        return null;
    }

    public GWidget hide() {
        return null;
    }

    public GListBox tabIndex(int index) {
        setTabIndex(index);
        return this;
    }

    public GListBox addItems(String[] items) {
        return addItems(items, null);
    }

    public GListBox addItems(String[] items, String selectedItem) {
        for (int i = 0; i < items.length; i++) {
            addItem(items[i]);
            if (items[i].equals(selectedItem)) {
                setSelectedIndex(i);
            }
        }
        return this;
    }

    public GListBox add(ChangeHandler handler) {
        addChangeHandler(handler);
        return this;
    }

    public GListBox add(FocusHandler handler) {
        addFocusHandler(handler);
        return this;
    }

    public GListBox add(BlurHandler handler) {
        addBlurHandler(handler);
        return this;
    }

    public GListBox enable() {
        setEnabled(true);
        return this;
    }

    public GListBox disable() {
        setEnabled(false);
        return this;
    }

    public GListBox add(ClickHandler handler) {
        addClickHandler(handler);
        return this;
    }

    public GListBox focus() {
        setFocus(true);
        return this;
    }
}
